var vlayer=null;

function initMap() {
    var _resolutions = [
//        1.40625, 
//        0.703125, 
//        0.3515625, 
        0.17578125, 
        0.087890625, 
        0.0439453125,
        0.02197265625, 
        0.010986328125, 
        0.0054931640625
//        0.00274658203125,
//        0.001373291015625, 
//        0.0006866455078125, 
//        0.00034332275390625,
//        0.000171661376953125, 
//        0.0000858306884765625, 
//        0.00004291534423828125,
//        0.00002145767211914062, 
//        0.00001072883605957031,
//        0.00000536441802978515, 
//        0.00000268220901489257
    ];
    
	var options = {
				resolutions: _resolutions,
//                minExtent: new OpenLayers.Bounds(-10, -10, 10, 10), 
//                maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90), 
				'numZoomLevels': 7,
                controls: [new OpenLayers.Control.MouseDefaults()]
	          };

	var map = new OpenLayers.Map('map', options);
	
	map.addControl(new OpenLayers.Control.PanZoomBar());

	var layer =	new OpenLayers.Layer.WMS( "Blue Marble", 
                    ["http://labs.metacarta.com/wms-c/Basic.py?","http://t2.labs.metacarta.com/wms-c/Basic.py?", "http://t1.labs.metacarta.com/wms-c/Basic.py?"], {layers: 'satellite' } );
//    var layer = new OpenLayers.Layer.Google( "Google Satellite", {type: G_HYBRID_MAP, 'minZoomLevel':4,'maxZoomLevel':7} );
//	var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", 
//	        "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'});
//    var layer = new OpenLayers.Layer.WorldWind( "Bathy", 
//		    	"http://worldwind25.arc.nasa.gov/tile/tile.aspx?", 36, 4,
//    			{T:"bmng.topo.bathy.200406"});
	
//	var tmlayer = new OpenLayers.Layer.WMS.Untiled("TM Path and Row", "/geoserver/wms?", {layers:'geo:tm_path_row',transparent:true, format:'image/png'});
//	var tm2layer = new OpenLayers.Layer.WFS.Untiled("Fire Statistic", "/geoserver/wms?", {layers:'geo:firestatistic',transparent:true, format:'image/png'});
	var tm2layer = new OpenLayers.Layer.WFS("Fire Statistic", "/geoserver/wfs?", {typename: "geo:firestatistic"}, {extractAttributes: true});
	tm2layer.preFeatureInsert = function(feature) {
		feature.style.strokeWidth= '2';
		feature.style.strokeColor = 'white';
		var _op = feature.attributes['count'] / 20.0
		if(_op > 1){
			_op = 1;
		}
		feature.style.fillOpacity = _op; 
		
//		feature.onClick = function(feature)
		
//		alert(feature.attributes.length());
	}
	
	tm2layer.onFeatureInsert = function(feature) {
		
	}
	
	map.events.register('click', map, function(e){
			var _f = tm2layer.getFeatureFromEvent(e);
			if(_f != null){
				dojo.xhrGet({
					url: 'process/imageInfo.do?path=' + _f.attributes['path'] + '&row=' + _f.attributes['row'],
					handleAs: 'json',
					load: function(response, ioArgs) {
//						dojo.byId('resultPanel').innerHTML = '';
						
						var _txt = '<div>Path=' + _f.attributes['path'] + ' Row=' + _f.attributes['row'] + '</div>';
						for(var _len=0; _len<response.result.length; _len++){
							_d = response.result[_len];
							_txt += '<div class="resultItem"><img src="images/point.png"/><a href="process/imageView.do?id=' + _d.id + '" target="_blank">' + _d.date + ', ' + _d.sensor + ', ' + _d.source + ', ' + _d.cloud + '</a></div>\n'; 
						}
						dojo.byId('resultPanel').innerHTML = _txt;
					},
					error: function(response, ioArgs) {
						alert('Error:' + ioArgs.xhr.status);
					}
				});
			}
			else{
				dojo.byId('resultPanel').innerHTML = '';
			}
		}
	);
	
    vlayer = new OpenLayers.Layer.Boxes("Boxes");

	map.addControl(new OpenLayers.Control.DataViewToolbar(vlayer));
//	map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
	
	map.addLayer(layer);
//	map.addLayer(tmlayer);
	map.addLayer(tm2layer);
	map.addLayer(vlayer);

	map.setCenter(new OpenLayers.LonLat(-100, 40), 0);
}

function changeExtentByBounds(bounds) {
	dojo.byId('minX').value = bounds.left.toFixed(2);
	dojo.byId('minY').value = bounds.bottom.toFixed(2);
	dojo.byId('maxX').value = bounds.right.toFixed(2);
	dojo.byId('maxY').value = bounds.top.toFixed(2);
}

function search(){
	if(vlayer.markers.length == 0){
		alert('No spatial extent');
		return;
	}
	
	var _bounds = vlayer.markers[0].bounds;
	var _start = dojo.byId('startDate').value;
	var _end = dojo.byId('endDate').value;
	
	var _url = 'process/search.do?minx=' + _bounds.left +
			 '&miny=' + _bounds.bottom +
			 '&maxx=' + _bounds.right + 
			 '&maxy=' + _bounds.top
	if(_start != ''){
		_url += '&start=' + _start;		
	}
	if(_end != ''){
		_url += '&end=' + _end;
	}

//	alert(_url);
	window.open(_url);
}